package starcoast.bocai.com.starcoast.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.activitys.movable.DetailActivity;
import starcoast.bocai.com.starcoast.activitys.movable.InDoorActivity;
import starcoast.bocai.com.starcoast.activitys.movable.OfficialActivity;
import starcoast.bocai.com.starcoast.activitys.movable.OutDoorActivity;
import starcoast.bocai.com.starcoast.activitys.movable.PostActivity;
import starcoast.bocai.com.starcoast.adapter.MovableAdapter;
import starcoast.bocai.com.starcoast.bean.ADInfo;
import starcoast.bocai.com.starcoast.common.BaseFragment;
import starcoast.bocai.com.starcoast.util.ViewFactory;
import starcoast.bocai.com.starcoast.view.CycleViewPager;
import starcoast.bocai.com.starcoast.view.MyListView;

/**
 * Created by admin on 2016/6/3.
 */
public class MovableFragment extends BaseFragment implements View.OnClickListener{
    @Bind(R.id.tv_title)
    TextView mTitleTv;
    @Bind(R.id.lv_movable)
    MyListView lv_movable;
    @Bind(R.id.movable_official)
    LinearLayout movable_official;
    @Bind(R.id.movable_outdoor)
    LinearLayout movable_outdoor;
    @Bind(R.id.movable_indoor)
    LinearLayout movable_indoor;
    @Bind(R.id.movable_topic)
    LinearLayout movable_topic;
    @Bind(R.id.movable_scroll)
    ScrollView movable_scroll;
    MovableAdapter adapter;
    View view;

    CycleViewPager cycleViewPager;
    private List<ImageView> views = new ArrayList<ImageView>();
    private List<ADInfo> infos = new ArrayList<ADInfo>();
    private List<String> imageList = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_movable, container, false);
        ButterKnife.bind(this, view);
        initView();
        initOnclick();
        return view;
    }
    public void initView(){
        imageList.add("http://img1.imgtn.bdimg.com/it/u=932695849,3126690420&fm=21&gp=0.jpg");
        imageList.add("http://img55.it168.com/ArticleImages/fnw/2016/0426/97642e6b-4b15-496e-b963-81f9d385c139.jpg");
        mTitleTv.setText(R.string.str_frag_movable);
        adapter = new MovableAdapter(getActivity());
        lv_movable.setAdapter(adapter);
        lv_movable.setFocusable(false);
        cycleViewPager = (CycleViewPager) getActivity().getFragmentManager()
                .findFragmentById(R.id.fragment_movable);
        configImageLoader();
        initialize();
        movable_scroll.smoothScrollTo(0,0);
    }
    public void initOnclick(){
        movable_official.setOnClickListener(this);
        movable_outdoor.setOnClickListener(this);
        movable_indoor.setOnClickListener(this);
        movable_topic.setOnClickListener(this);
        lv_movable.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(getActivity(), DetailActivity.class));
            }
        });
    }
    private void initialize() {
        for(int i = 0; i < imageList.size(); i ++){
            ADInfo info = new ADInfo();
            info.setUrl(imageList.get(i));
            info.setContent("图片-->" + i );
            infos.add(info);
        }
        // 将最后一个ImageView添加进来
        views.add(ViewFactory.getImageView(getActivity(), infos.get(infos.size() - 1).getUrl()));
        for (int i = 0; i < infos.size(); i++) {
            views.add(ViewFactory.getImageView(getActivity(), infos.get(i).getUrl()));
        }
        // 将第一个ImageView添加进来
        views.add(ViewFactory.getImageView(getActivity(), infos.get(0).getUrl()));

        // 设置循环，在调用setData方法前调用
        cycleViewPager.setCycle(true);

        // 在加载数据前设置是否循环
        cycleViewPager.setData(views, infos, mAdCycleViewListener);
        //设置轮播
        cycleViewPager.setWheel(true);

        // 设置轮播时间，默认5000ms
        cycleViewPager.setTime(2000);
        //设置圆点指示图标组居中显示，默认靠右
        cycleViewPager.setIndicatorCenter();
    }
    private CycleViewPager.ImageCycleViewListener mAdCycleViewListener = new CycleViewPager.ImageCycleViewListener() {

        @Override
        public void onImageClick(ADInfo info, int position, View imageView) {
            if (cycleViewPager.isCycle()) {

            }

        }

    };

    /**
     * 配置ImageLoder
     */
    private void configImageLoader() {
        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                // .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.movable_official:
                startActivity(new Intent(getActivity(), OfficialActivity.class));
                break;
            case R.id.movable_outdoor:
                startActivity(new Intent(getActivity(), OutDoorActivity.class));
                break;
            case R.id.movable_indoor:
                startActivity(new Intent(getActivity(), InDoorActivity.class));
                break;
            case R.id.movable_topic:
                startActivity(new Intent(getActivity(), PostActivity.class));
                break;
        }
    }
}
